package com.zhc.dejingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.common.result.Result;
import com.zhc.dejingapi.common.result.ResultCodeEnum;
import com.zhc.dejingapi.entity.QingZbProcessrouteEntity;
import com.zhc.dejingapi.entity.QingZbProcesswithrouteEntity;
import com.zhc.dejingapi.entity.vo.ProcessRouteVo;
import com.zhc.dejingapi.mapper.QingZbProcessrouteMapper;
import com.zhc.dejingapi.mapper.QingZbProcesswithrouteMapper;
import com.zhc.dejingapi.service.QingZbProcessrouteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhcvo.ProcessRouteVo;
 * import com.zhc.dejin
 * @since 2024-09-11
 */
@Service
public class QingZbProcessrouteServiceImpl extends ServiceImpl<QingZbProcessrouteMapper, QingZbProcessrouteEntity> implements QingZbProcessrouteService {

    @Resource
    private QingZbProcessrouteMapper qingZbProcessrouteMapper;
    @Resource
    private QingZbProcesswithrouteMapper qingZbProcesswithrouteMapper;
    @Override
    public Result addProcessRoute(ProcessRouteVo processRouteVo) {
        QueryWrapper<QingZbProcessrouteEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("process_route_code",processRouteVo.getProcessRouteCode());
        Long l = qingZbProcessrouteMapper.selectCount(queryWrapper);
        if(l>0){
            if(processRouteVo.getMainId()!=null){
                QingZbProcessrouteEntity qingZbProcessrouteEntity = new QingZbProcessrouteEntity();

                qingZbProcessrouteEntity.setProcessRouteCode(processRouteVo.getProcessRouteCode());
                qingZbProcessrouteEntity.setProcessRouteName(processRouteVo.getProcessRouteName());
                qingZbProcessrouteEntity.setProcessRouteVersions(processRouteVo.getProcessRouteVersions());
                qingZbProcessrouteEntity.setVersionsDesc(processRouteVo.getVersionsDesc());
                qingZbProcessrouteEntity.setRemark(processRouteVo.getRemark());
                QueryWrapper<QingZbProcessrouteEntity> queryWrapper1=new QueryWrapper<>();
                queryWrapper1.eq("main_id",processRouteVo.getMainId());
                qingZbProcessrouteMapper.update(qingZbProcessrouteEntity,queryWrapper1);
                 QueryWrapper<QingZbProcesswithrouteEntity> queryWrapper2=new QueryWrapper<>();
                queryWrapper2.eq("route_id",processRouteVo.getMainId());
                qingZbProcesswithrouteMapper.delete(queryWrapper2);
                Long mainId = processRouteVo.getMainId();

                for (int i=0;i<processRouteVo.getProcesswithrouteList().size(); i++) {
                    QingZbProcesswithrouteEntity qingZbProcesswithrouteEntity =processRouteVo.getProcesswithrouteList().get(i);
                    qingZbProcesswithrouteEntity.setRouteId(mainId);
                    qingZbProcesswithrouteEntity.setLineNum(i+1);
                    int insert1 = qingZbProcesswithrouteMapper.insert(qingZbProcesswithrouteEntity);

                }

                return Result.ok(null);
            }else{
                return Result.build(null, ResultCodeEnum.PROCESS_ROUTE_EXIST);
            }

        }else {
            QingZbProcessrouteEntity qingZbProcessrouteEntity = new QingZbProcessrouteEntity();
            qingZbProcessrouteEntity.setProcessRouteCode(processRouteVo.getProcessRouteCode());
            qingZbProcessrouteEntity.setProcessRouteName(processRouteVo.getProcessRouteName());
            qingZbProcessrouteEntity.setProcessRouteVersions(processRouteVo.getProcessRouteVersions());
            qingZbProcessrouteEntity.setVersionsDesc(processRouteVo.getVersionsDesc());
            qingZbProcessrouteEntity.setRemark(processRouteVo.getRemark());
            int insert = qingZbProcessrouteMapper.insert(qingZbProcessrouteEntity);
            int j=0;
            if(insert>0){

                Long mainId = qingZbProcessrouteEntity.getMainId();
                for (int i=0;i<processRouteVo.getProcesswithrouteList().size(); i++) {
                    QingZbProcesswithrouteEntity qingZbProcesswithrouteEntity =processRouteVo.getProcesswithrouteList().get(i);
                    qingZbProcesswithrouteEntity.setRouteId(mainId);
                    qingZbProcesswithrouteEntity.setLineNum(i+1);
                    int insert1 = qingZbProcesswithrouteMapper.insert(qingZbProcesswithrouteEntity);
                    if(insert1>0){
                        j++;
                    }
                }

                }
            if(j==processRouteVo.getProcesswithrouteList().size()){
                return Result.build(null, ResultCodeEnum.PROCESS_ROUTE_ADD_SUCCESS);
            }else{
                return Result.build(null, ResultCodeEnum.PROCESS_ROUTE_ADD_FAIL);
            }
            }
        }

    @Override
    public Result selectProcessRoute(QingZbProcessrouteEntity qingZbProcessrouteEntity) {
        List<QingZbProcessrouteEntity> qingProcessrouteEntities = qingZbProcessrouteMapper.selectList(null);
        return Result.build(qingProcessrouteEntities, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result selectProcessWithRoute(String processRouteNum) {
        QueryWrapper<QingZbProcessrouteEntity> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("process_route_code",processRouteNum);
        QingZbProcessrouteEntity qingZbProcessrouteEntity = qingZbProcessrouteMapper.selectOne(queryWrapper1);
        QueryWrapper<QingZbProcesswithrouteEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("route_id", qingZbProcessrouteEntity.getMainId()).orderByAsc("line_num");
        List<QingZbProcesswithrouteEntity> qingProcesswithrouteEntities = qingZbProcesswithrouteMapper.selectList(queryWrapper);
        ProcessRouteVo processRouteVo = new ProcessRouteVo();
        processRouteVo.setMainId(qingZbProcessrouteEntity.getMainId());
        processRouteVo.setProcessRouteCode(qingZbProcessrouteEntity.getProcessRouteCode());
        processRouteVo.setProcessRouteName(qingZbProcessrouteEntity.getProcessRouteName());
        processRouteVo.setProcessRouteVersions(qingZbProcessrouteEntity.getProcessRouteVersions());
        processRouteVo.setVersionsDesc(qingZbProcessrouteEntity.getVersionsDesc());
        processRouteVo.setRemark(qingZbProcessrouteEntity.getRemark());
        processRouteVo.setProcesswithrouteList(qingProcesswithrouteEntities);

        return Result.ok(processRouteVo);
    }

    @Override
    public Result deleteProcessRoute(String processRouteNum) {
        QueryWrapper<QingZbProcessrouteEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("process_route_code",processRouteNum);
        QingZbProcessrouteEntity qingZbProcessrouteEntity = qingZbProcessrouteMapper.selectOne(queryWrapper);
        int deleteProcess=0;
        if(qingZbProcessrouteEntity !=null){
            QueryWrapper<QingZbProcesswithrouteEntity> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("route_id", qingZbProcessrouteEntity.getMainId());
            int delete = qingZbProcesswithrouteMapper.delete(queryWrapper1);
            if(delete>0){
                deleteProcess = qingZbProcessrouteMapper.delete(queryWrapper);
            }


        }
        if(deleteProcess>0){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }


}


